import fs from 'node:fs';
import { program } from 'commander';

import * as Enums from '../utils/Enums';
import Printer from '../utils/Printer';
import { InternalApps, InternalServices, ExternalApps } from '../config/Modules';
import PathHelper from '../utils/PathHelper';

const listOptions = () => {
  const moduleGroups = [
    { typeOf: Enums.ModuleType.App, modules: [...Object.values(InternalApps), ...Object.values(ExternalApps)] },
    { typeOf: Enums.ModuleType.Service, modules: [...Object.values(InternalServices)] },
  ];
  const moduleTypes = Object.values<string>(Enums.ModuleType);
  const proxyEnvFiles = fs.readdirSync(PathHelper.getPathRelativeToRoot('../proxyService/src/config'));
  const environments = proxyEnvFiles.map((filename) => filename.split('.')[0])
    .filter((filename) => filename !== 'index' && filename !== 'processEnv');

  Printer.describe('-t --type  ', '', moduleTypes);
  Printer.describe('-m --module', '', moduleGroups);
  Printer.describe('-e --env   ', '', environments);
  Printer.describe('`cli local`', 'example:', 'cli local -t app     -m main   -e ppc-local');
  Printer.describe('`cli build`', 'example:', 'cli build -t service -m proxy  -e staging');
};

export default program
  .command('ls-options')
  .description('列出运行、构建 app 或 service 可用的参数清单')
  .action(listOptions);
